package com.jpx.jxcbiz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jpx.common.dto.TGoodsDto;
import com.jpx.common.utils.ExcelBean;
import com.jpx.common.utils.ExcelUtils;
import com.jpx.common.vo.GoodsDto;
import com.jpx.jxcbiz.entity.TGoods;
import com.jpx.jxcbiz.entity.TGoodstype;
import com.jpx.jxcbiz.mapper.TGoodsMapper;
import com.jpx.jxcbiz.service.ITGoodsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jpx.jxcbiz.service.ITGoodstypeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mht
 * @since 2020-02-24
 */
@Service
public class TGoodsServiceImpl extends ServiceImpl<TGoodsMapper, TGoods> implements ITGoodsService {

    @Resource
    private TGoodsMapper tGoodsMapper;

    @Autowired
    private ITGoodstypeService goodstypeService;

    @Override
    public List getGoodsList(String name,String bname,Integer typeId) {
        return tGoodsMapper.getGoodsList(name, bname,typeId);
    }
    TGoodsMapper goodsMapper;

    @Override
    public List<TGoodsDto> list(TGoodsDto goodsDto) {
        return goodsMapper.list(goodsDto);
    }

    @Override
    public List<TGoods> goodList(TGoodsDto goodsDto) {
        return goodsMapper.goodList(goodsDto);
    }

    @Override
    public XSSFWorkbook downLoadGoodsExcel(GoodsDto goodsDto) throws Exception {
        PageHelper.startPage(goodsDto.getPageNo(),goodsDto.getPageSize());
        Wrapper wrapper = new EntityWrapper();
        if (StringUtils.isNotBlank(goodsDto.getName())){
            wrapper.like("name",goodsDto.getName());
        }
        List<TGoods> list = this.selectList(wrapper);
        PageInfo<TGoods> pageInfo = new PageInfo<>(list);
        Map<Integer,List<ExcelBean>> map = new HashMap<Integer,List<ExcelBean>>();
        List<ExcelBean> excelBeans = new ArrayList<>();
        excelBeans.add(new ExcelBean("商品编码","code",0));
        excelBeans.add(new ExcelBean("商品名称","name",0));
//        excelBeans.add(new ExcelBean("类别","bname",0));
        excelBeans.add(new ExcelBean("商品型号","model",0));
        excelBeans.add(new ExcelBean("库存总量","minNum",0));
        excelBeans.add(new ExcelBean("销售数","inventoryQuantity",0));
        excelBeans.add(new ExcelBean("上次进价","lastPurchasingPrice",0));
        excelBeans.add(new ExcelBean("平均售价","purchasingPrice",0));
        excelBeans.add(new ExcelBean("销售价","sellingPrice",0));
        map.put(0,excelBeans);

        return ExcelUtils.createExcelFile(TGoods.class,pageInfo.getList(),map,"商品库存列表");
    }

    @Override
    public void uplodExcel(MultipartFile file) throws Exception{
        List<List<Object>> bankListByExcel = ExcelUtils.getBankListByExcel(file.getInputStream(),
                file.getOriginalFilename());
        List<TGoods> list = new ArrayList<>();

        for (List<Object> objects : bankListByExcel){
            TGoods goods = new TGoods();
            goods.setCode(objects.get(0).toString());
            goods.setName(objects.get(1).toString());
            goods.setModel(objects.get(2).toString());
            goods.setMinNum(Integer.valueOf(objects.get(3).toString()));
            goods.setInventoryQuantity(Integer.valueOf(objects.get(4).toString()));
            goods.setLastPurchasingPrice(Float.valueOf(objects.get(5).toString()));
            goods.setPurchasingPrice(Float.valueOf(objects.get(6).toString()));
            goods.setSellingPrice(Float.valueOf(objects.get(7).toString()));

            goods.setState(0);
            list.add(goods);
        }
        this.insertBatch(list);
    }
}
